package com.cicigodd.brick.plug.system.user.controller;

import com.cicigodd.brick.plug.annotation.resource.DeleteResource;
import com.cicigodd.brick.plug.annotation.resource.GetResource;
import com.cicigodd.brick.plug.annotation.resource.PostResource;
import com.cicigodd.brick.plug.annotation.resource.PutResource;
import com.cicigodd.brick.plug.common.entity.SimpleUserInfo;
import com.cicigodd.brick.plug.common.response.PageResult;
import com.cicigodd.brick.plug.common.response.Result;
import com.cicigodd.brick.plug.common.response.ResultGenerator;

import com.cicigodd.brick.plug.system.auth.api.AuthServiceContext;
import com.cicigodd.brick.plug.system.role.request.SysRoleDetailRequest;
import com.cicigodd.brick.plug.system.user.request.*;
import com.cicigodd.brick.plug.system.user.response.SysUserDetailResponse;
import com.cicigodd.brick.plug.system.user.response.SysUserListResponse;
import com.cicigodd.brick.plug.system.user.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


@Api(tags = "系统用户管理")
@RestController
public class SysUserController {

    @Resource
    private SysUserService sysUserService;


    @ApiOperation(value = "重置密码", notes = "系统用户管理重置密码")
    @PostResource(name = "重置密码", path = "/sysUser/resetPwd")
    public Result resetPwd(@RequestBody @Validated SysUserResetRequest sysUserResetRequest) {
        sysUserService.resetPwd(sysUserResetRequest);
        return ResultGenerator.successResult();
    }


    @ApiOperation(value = "更新密码", notes = "系统用户管理更新密码")
    @PostResource(name = "更新密码", path = "/sysUser/updatePassword")
    public Result updatePassword(@RequestBody @Validated SysUserUpdatePwdRequest sysUserUpdatePwdRequest) {
        sysUserService.updatePassword(sysUserUpdatePwdRequest);
        return ResultGenerator.successResult("密码修改成功下次登录生效");
    }

    @ApiOperation(value = "系统用户管理添加", notes = "系统用户管理添加")
    @PostResource(name = "系统用户管理添加", path = "/sysUser/add")
    public Result add(@RequestBody @Validated SysUserAddRequest sysUserAddRequest) {
        sysUserService.add(sysUserAddRequest);
        return ResultGenerator.successResult();
    }


    @ApiOperation(value = "系统用户管理删除", notes = "根据id删除")
    @DeleteResource(name = "系统用户管理删除", path = "/sysUser/delete")
    public Result delete(@Validated SysUserDeleteRequest sysUserDeleteRequest) {
        sysUserService.delete(sysUserDeleteRequest);
        return ResultGenerator.successResult();
    }


    @ApiOperation(value = "系统用户管理编辑", notes = "系统用户管理编辑")
    @PutResource(name = "系统用户管理编辑", path = "/sysUser/edit")
    public Result edit(@RequestBody @Validated SysUserEditRequest sysUserEditRequest) {
        sysUserService.edit(sysUserEditRequest);
        return ResultGenerator.successResult();
    }


    @ApiOperation(value = "系统用户管理详情", notes = "根据id查询")
    @GetResource(name = "系统用户管理详情", path = "/sysUser/detail")
    public Result<SysUserDetailResponse> detail(SysUserDetailRequest sysUserDetailRequest) {
        SysUserDetailResponse detail = sysUserService.detail(sysUserDetailRequest);
        return ResultGenerator.successResult(detail);
    }


    @ApiOperation(value = "系统用户管理分页列表", notes = "系统用户管理分页列表")
    @GetResource(name = "系统用户管理分页列表", path = "/sysUser/list")
    public Result<PageResult<SysUserListResponse>> list(SysUserListRequest sysUserListRequest) {
        PageResult<SysUserListResponse> pageList = sysUserService.list(sysUserListRequest);
        return ResultGenerator.successResult(pageList);
    }

    @ApiOperation(value = "获取个人信息", notes = "获取个人信息")
    @GetResource(name = "获取个人信息", path = "/sysUser/getUserInfo")
    public Result<SimpleUserInfo> getUserInfo() {
        Integer userId = AuthServiceContext.me().getLoginUser().getUserId();
        SimpleUserInfo detail = sysUserService.getUserInfo(userId);
        return ResultGenerator.successResult(detail);
    }

    @ApiOperation(value = "获取用户绑定的角色", notes = "获取用户绑定的角色")
    @GetResource(name = "获取用户绑定的角色", path = "/sysRole/getRoleIdsByUserId")
    public Result<List<Integer>> getRoleIdsByUserId(SysRoleDetailRequest sysRoleDetailRequest) {
        List<Integer> list =  sysUserService.getRoleIdsByUserId(sysRoleDetailRequest.getId());
        return ResultGenerator.successResult(list);
    }


    @ApiOperation(value = "更新用户个人信息", notes = "更新用户个人信息")
    @PutResource(name = "更新用户个人信息", path = "/sysUser/updateUserInfo")
    public Result updateUserInfo(@RequestBody @Validated SysUserUpdateUserInfoRequest SysUserUpdateUserInfoRequest) {
        sysUserService.updateUserInfo(SysUserUpdateUserInfoRequest);
        return ResultGenerator.successResult();
    }
}
